﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ModelViewParameterValidator.h"
#include "DbObjectUtils.h"
#include "GcmpBuiltInParameterDefinitions.h"
#include "IDocument.h"
#include "IElementParameters.h"
#include "IGenericElement.h"
#include "IModelView.h"
#include "IModelViewSymbolNew.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

DBOBJECT_DATA_DEFINE(ModelViewParameterValidator)
{
}

OwnerPtr<IParameterValidator> ModelViewParameterValidator::Clone() const
{
    return NEW_AS_OWNER_PTR(ModelViewParameterValidator);
}

bool ModelViewParameterValidator::Validate(const IParameter* pNewParameter, const IElement* pElement, std::wstring* pErrorMessage) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pNewParameter && pElement, L"传入参数存在空值",L"GDMPLab",L"2024-03-30");

    //pElement：可能是视图，也可能是视图标志，为视图标志时从标志取出相应视图
    const IModelView* pModelView = quick_cast<const IModelView>(pElement);
    if (!pModelView)
    {
        const IGenericElement *pGenericElement = quick_cast<const IGenericElement>(pElement);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pGenericElement, L"invalid pGenericElement",L"GDMPLab",L"2024-03-30");

        const IModelViewSymbolNew *pModelViewSymbol = quick_cast<IModelViewSymbolNew>(pGenericElement->GetExternalObject());
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelViewSymbol, L"invalid pModelViewSymbol",L"GDMPLab",L"2024-03-30");

        ElementId modelViewId = pModelViewSymbol->GetModelViewId();
        IDocument* pDoc = pElement->GetDocument();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
        pModelView = quick_cast<IModelView>(pDoc->GetElement(modelViewId));
    }
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    const IElementParameters* pElementParameter = pElement->GetElementParameters();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElementParameter, L"pElementParameters为空",L"GDMPLab",L"2024-03-30");

    if (pNewParameter->GetParameterDefinitionUid() == PARAMETER_UID(ElementNameBuiltInParameter))
    {
        return ValidateName(pModelView, pNewParameter->GetValueAsString(), pErrorMessage);
    }

    return true;
}

bool ModelViewParameterValidator::ValidateName(const IModelView* pModelView, const std::wstring& newName, std::wstring* pErrorMessage) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pModelView->GetDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    std::wstring viewType = pModelView->GetUserDataId();

    const auto& modelviews = IModelView::GetAllModelViews(pDoc);
    FOR_EACH(pModelView, modelviews)
    {
        if (pModelView->GetUserDataId() != viewType)
            continue;
        if (pModelView->GetName() == newName)
        {
            if (pErrorMessage)
            {
                *pErrorMessage = GBMP_TR(L"输入的视图名称已存在，请重新输入。");
            }
            return false;
        }
    }

    return true;
}

//////////////////////////////////////////////////////////////////////////
/// ModelViewParameterValidatorBehavior
DBOBJECT_DATA_DEFINE(ModelViewParameterValidatorOverride)
{
    m_ParameterUid = UniIdentity::InvalidUid;
}

ModelViewParameterValidatorOverride::ModelViewParameterValidatorOverride(UniIdentity parameterUid)
    : m_ParameterUid(parameterUid)
{

}

UniIdentity ModelViewParameterValidatorOverride::GetUniIdentity() const
{
    return GetParameterUid__();
}

bool ModelViewParameterValidatorOverride::ShouldOverrideIsModifiable() const
{
    return false;
}

bool ModelViewParameterValidatorOverride::IsModifiable() const
{
    return true;
}

bool ModelViewParameterValidatorOverride::ShouldOverrideIsUserVisible() const
{
    return false;
}

bool ModelViewParameterValidatorOverride::IsUserVisible() const
{
    return true;
}

bool ModelViewParameterValidatorOverride::ShouldOverrideGetName() const
{
    return false;
}

std::wstring ModelViewParameterValidatorOverride::GetName() const
{
    return L"";
}

bool ModelViewParameterValidatorOverride::ShouldOverrideGetValidator() const
{
    return true;
}

const IParameterValidator* ModelViewParameterValidatorOverride::GetValidator() const
{
    static ModelViewParameterValidator validator;
    return &validator;
}

//////////////////////////////////////////////////////////////////////////
/// ModelViewDisableTopPlaneOffsetValidatorBehavior
DBOBJECT_DATA_DEFINE(ModelViewDisableTopPlaneOffsetValidatorOverride)
{
    m_ParameterUid = UniIdentity::InvalidUid;
}

ModelViewDisableTopPlaneOffsetValidatorOverride::ModelViewDisableTopPlaneOffsetValidatorOverride(UniIdentity parameterUid)
    : m_ParameterUid(parameterUid)
{

}

UniIdentity ModelViewDisableTopPlaneOffsetValidatorOverride::GetUniIdentity() const
{
    return GetParameterUid__();
}

bool ModelViewDisableTopPlaneOffsetValidatorOverride::ShouldOverrideIsModifiable() const
{
    return true;
}

bool ModelViewDisableTopPlaneOffsetValidatorOverride::IsModifiable() const
{
    return false;
}

bool ModelViewDisableTopPlaneOffsetValidatorOverride::ShouldOverrideIsUserVisible() const
{
    return false;
}

bool ModelViewDisableTopPlaneOffsetValidatorOverride::IsUserVisible() const
{
    return true;
}

bool ModelViewDisableTopPlaneOffsetValidatorOverride::ShouldOverrideGetName() const
{
    return false;
}

std::wstring ModelViewDisableTopPlaneOffsetValidatorOverride::GetName() const
{
    return L"";
}

bool ModelViewDisableTopPlaneOffsetValidatorOverride::ShouldOverrideGetValidator() const
{
    return false;
}

const IParameterValidator* ModelViewDisableTopPlaneOffsetValidatorOverride::GetValidator() const
{
    return nullptr;
}
